const app = getApp()
import { home, getVenue, backlogListMini, backlogFinishMini, resUser } from '../../api/index'
import { getYMDHMS } from '../../utils/util'
import { wxId } from "../../utils/config"
Page({

  /* 页面的初始数据 */
  data: {
    CGname: '',
    nickName: '',
    date: '',
    wxId:wxId,
    // headList: [{
    //   name: '营业收入',
    //   value: '0'
    // }, {
    //   name: '会员开卡',
    //   value: '0'
    // }, {
    //   name: '会员充值',
    //   value: '0'
    // }, {
    //   name: '在场人数',
    //   value: '0'
    // }],
    userList: {},
    headList: [{
      name: '今日营收',
      value: '0'
    }, {
      name: '今日订单',
      value: '0'
    }, {
      name: '今日入场',
      value: '0'
    }],
    iconListb: [],
    iconList: [{
      name: '场地预定',
      id: 0,
      icon: '../../static/images/home_nav_1.png'
    },
    {
      name: '资金流水',
      id: 8,
      icon: '../../static/images/home_nav_jiesuan.png'
    },
    {
      name: '经营分析',
      id: 2,
      icon: '../../static/images/home_nav_jiankong.png'
    },
    // {
    //   name: '结算',
    //   id: 2,
    //   icon: '../../static/images/home_nav_3.png'
    // },
     {
      name: '门禁管控',
      id: 3,
      icon: '../../static/images/home_nav_4.png'
    }, {
      name: '灯光管控',
      id: 4,
      icon: '../../static/images/home_nav_5.png'
    }, {
      name: '监控设备',
      id: 5,
      icon: '../../static/images/home_nav_6.png'
    }, {
      name: '扫码核验',
      id: 7,
      icon: '../../static/images/home_nav_8.png'
    },
    {
      name: '会员核验',
      id: 10,
      icon: '../../static/images/home_nav_dengkong.png'
    },
     {
      name: '会员管理',
      id: 1,
      icon: '../../static/images/home_nav_2.png'
    },{
      name: '意见反馈',
      id: 6,
      icon: '../../static/images/home_nav_7.png'
    }],
    page: 0,    //页码
    is: true,   //控制是否还能切换下一页
    list: [],
    totalCount: 0
  },
  login() {
    wx.navigateTo({
      url: '/pages/login/index'
    })
  },
  jumps(e) {
    if (!this.data.nickName) {
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 500);
      return wx.showToast({ title: '请先登录', icon: 'none', duration: 2000 })
    }
    let index = e.currentTarget.dataset.index
    let value = e.currentTarget.dataset.value
    if(value && value > 0) {
      if (index == 2) {
        wx.navigateTo({
          url: '/pages/bepresent/index'
        })
      } else {
        wx.navigateTo({
          url: '/pages/invoiceList/index'
        })
      }
    }
  },
  chuli(e){
    let that = this
    backlogFinishMini({
      param: {
        venueId: wx.getStorageSync('GYMID'),
        id: e.currentTarget.dataset.id,
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      wx.showToast({
        title: '处理完成',
        icon: 'success',
        duration: 1000,
        success(res) {
          setTimeout(() => {
            that.setData({ list: [], totalCount: 0 })
            that.getList()
          }, 100);
        }
      })
    })
  },
  jumpa(e) {
    if (!this.data.nickName) {
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 500);
      return wx.showToast({ title: '请先登录', icon: 'none', duration: 2000 })
    }
    let index = e.currentTarget.dataset.index
    if (index == 0) {
      wx.navigateTo({
        url: '/pages/siteReservation/index'
      })
    } else if (index == 1) {
      wx.navigateTo({
        url: '/pages/member/index'
      })
    } else if (index == 2) {
      wx.navigateTo({
        url: '/pages/analysis/index'
      })
    } else if (index == 3) {
      wx.navigateTo({
        url: '/pages/accessControl/index'
      })
    } else if (index == 4) {
      wx.navigateTo({
        url: '/pages/lamp/index'
      })
    } else if (index == 5) {
      wx.navigateTo({
        url: '/pages/camera/index'
      })
    } else if (index == 6) {
      wx.navigateTo({
        url: '/pages/feedbackList/index'
      })
    } else if (index == 7) {
      wx.scanCode({
        success(res) {
          wx.navigateTo({
            url: '/pages/verification/details?code=' + res.result
          })
        }
      })
    } else if (index == 8) {
      wx.navigateTo({
        url: '/pages/fund/index'
      })
    } else if (index == 10) {
      wx.navigateTo({
        url: '/pages/verification/list'
      })
    } 
  },
  jumpb() {
    if (!this.data.nickName) {
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 500);
      return wx.showToast({ title: '请先登录', icon: 'none', duration: 2000 })
    }
    wx.navigateTo({
      url: '/pages/todolist/index'
    })
  },
  jumpp() {
    if (!this.data.nickName) {
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 500);
      return wx.showToast({ title: '请先登录', icon: 'none', duration: 2000 })
    }
    wx.navigateTo({
      url: '/pages/personal/index'
    })
  },
  // 跳转详情
  jump(e) {
    if (!this.data.nickName) {
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 500);
      return wx.showToast({ title: '请先登录', icon: 'none', duration: 2000 })
    }
    wx.navigateTo({
      url: '/pages/feedback/index?id=' + e.currentTarget.dataset.id,
    })
  },
  // 列表
  getList() {
    backlogListMini({
      pagination: {
        firstQueryTime:getYMDHMS(new Date()),
        page: 1,
        rows: 5
      },
      param: {
        venueId: wx.getStorageSync('GYMID'),
        type: '1',
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      this.setData({ list: res.recordList, totalCount: res.totalCount })
    })
  },
  // 获取场馆 更新场馆id以及场馆名称
  getCG() {
    var that = this
    getVenue({
      param: {
        lat: '',
        lng: '',
        openStatus: '1'
      }
    }).then(res => {
      that.setData({ CGname: res.recordList[0].name })
      wx.setStorageSync('venueName', res.recordList[0].name)
      wx.setStorageSync('GYMID', res.recordList[0].id)
      that.getHome()
      that.getList()
    })
    // wx.getLocation({
    //   type: 'gcj02',
    //   success(res) {
    //     getVenue({
    //       param: {
    //         lat: res.latitude,
    //         lng: res.longitude
    //       }
    //     }).then(res => {    //用户同意授权位置
    //       console.log('用户同意授权位置')
    //       var distance = 0
    //       var ary = {}
    //       res.recordList.forEach((item, i) => {
    //         if (i == 0) {
    //           distance = item.distance
    //           ary = item
    //         }
    //         if (item.distance < distance) {
    //           distance = item.distance
    //           ary = item
    //         }
    //       })
    //       that.setData({ CGname: ary.name })
    //       wx.setStorageSync('venueName', ary.name)
    //       wx.setStorageSync('GYMID', ary.id)
    //       that.getHome()
    //       that.getList()
    //     })
    //   },
    //   fail(err) {    //用户拒绝授权位置
    //     console.log('用户拒绝授权位置')
    //     getVenue({
    //       param: {
    //         lat: '',
    //         lng: ''
    //       }
    //     }).then(res => {
    //       that.setData({ CGname: res.recordList[0].name })
    //       wx.setStorageSync('venueName', res.recordList[0].name)
    //       wx.setStorageSync('GYMID', res.recordList[0].id)
    //       that.getHome()
    //       that.getList()
    //     })
    //   }
    // })
  },
  // 获取日期
  getDate() {
    let date = new Date();
    let nnn = date.getFullYear()
    let yyy = date.getMonth() + 1
    let rrr = date.getDate()
    let xq = date.getDay()
    let data = ''
    switch (xq) {
      case 1:
        data = '周一'
        break;
      case 2:
        data = '周二'
        break;
      case 3:
        data = '周三'
        break;
      case 4:
        data = '周四'
        break;
      case 5:
        data = '周五'
        break;
      case 6:
        data = '周六'
        break;
      case 0:
        data = '周日'
        break;
    }
    let res = nnn + '年' + yyy + '月' + rrr + '日' + ' ' + data
    this.setData({
      date: res
    })
  },
  getHome() {
    resUser({
      param: {}
    }).then(res => {
      if (res.errorCode != '000000') return
      let obj = res.record
      let iconList = this.data.iconList
      let list = []
      iconList.forEach((s)=> {
        if(s.name == '场地预定' && obj['OMPHSite']) {
          list.push(s)
        } else if(s.name == '资金流水' && obj['OMPHFlowFunds']) {
          list.push(s)
        } else if(s.name == '经营分析' && obj['OMPHBusinessAnalysis']) {
          list.push(s)
        } else if(s.name == '门禁管控' && obj['OMPHDevEntrance']) {
          list.push(s)
        } else if(s.name == '灯光管控' && obj['OMPHLampControl']) {
          list.push(s)
        } else if(s.name == '监控设备' && obj['OMPHSupervisory']) {
          list.push(s)
        } else if(s.name == '扫码核验' && obj['OMPHCodeVerification']) {
          list.push(s)
        } else if(s.name == '会员核验' && obj['OMPHMebVerification']) {
          list.push(s)
        }  else if(s.name == '会员管理' && obj['OMPHMember']) {
          list.push(s)
        }  else if(s.name == '意见反馈' && obj['OMPHFeedback']) {
          list.push(s)
        } 
      })
      this.setData({
        iconListb: list,
        userList: res.record
      })
    })
    home({
      param: {
        venueId: wx.getStorageSync('GYMID'),
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      this.setData({
        headList: [{
          name: '今日营收',
          value: res.record.income 
        }, {
          name: '今日订单',
          value: res.record.orderCount
        }, {
          name: '今日入场',
          value: res.record.peopleNum
        }],
      })
    })
  },
  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    this.getDate()
  },
  

  /* 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {

  },

  /* 生命周期函数--监听页面显示 */
  onShow: function () {
    this.setData({ CGname: wx.getStorageSync('venueName'), nickName: wx.getStorageSync('nickName') })
    if (app.globalData.login) {
      if (wx.getStorageSync('userId') && !wx.getStorageSync('venueName')) {
        this.getCG()
      } else if (wx.getStorageSync('userId') && wx.getStorageSync('venueName')&& wx.getStorageSync('GYMID')) {
        this.getHome()
        this.getList()
      }
    } else {
      let res = setInterval(() => {
        if (app.globalData.login) {
          if (wx.getStorageSync('userId') && !wx.getStorageSync('venueName')) {
            this.getCG()
          } else if (wx.getStorageSync('userId') && wx.getStorageSync('venueName')&& wx.getStorageSync('GYMID')) {
            this.getHome()
            this.getList()
          }
          clearInterval(res)
        }
      }, 500)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ returnedValue: '', show: false })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})